package com.bradycorp.google;

import java.net.URL;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;

public class CCalendarReportSample {

	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		CalendarService myService = new CalendarService("");
		System.out.println("Username: ");
		String username = scanner.nextLine() +
		 "@bradycorp.com";
		System.out.println("Password: ");
		String password = scanner.nextLine();

		myService.setUserCredentials(username, password);

		// URL feedUrl = new
		// URL(
		// );
		URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/owncalendars/full");

		boolean repeat = false;

		do {
			System.out.println("Start Month");
			String sStartMonth = "08";// scanner.next();

			System.out.println("Start Day");
			String sStartDay = "02";// scanner.next();

			System.out.println("End Month");
			String sEndMonth = "08"; // scanner.next();

			System.out.println("End Day");
			String sEndDay = "08";// scanner.next();

			CalendarQuery myQuery = new CalendarQuery(feedUrl);
			myQuery.setMinimumStartTime(DateTime.parseDateTime("2010-"
					+ sStartMonth + "-" + sStartDay + "T00:00:00"));
			myQuery.setMaximumStartTime(DateTime.parseDateTime("2010-"
					+ sEndMonth + "-" + sEndDay + "T23:59:59"));

			myQuery.setMaxResults(100);

			// Send the request and receive the response:
			CalendarEventFeed resultFeed = myService.query(myQuery,
					CalendarEventFeed.class);
			processEventFeed(resultFeed);

			System.out.println("ReRun? ");
			String answer = scanner.next();
			repeat = answer.toLowerCase().startsWith("y");

		} while (repeat);

	}

	private static void processEventFeed(CalendarEventFeed resultFeed) {

		long totalDuration = 0;
		int weeks = 1;
		int minutesInWeek = 2250;

		System.out.println(resultFeed.getTotalResults() + " Total Results ");

		List<String> lstTakeAways = new LinkedList<String>();
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
			CalendarEventEntry entry = resultFeed.getEntries().get(i);
			String sContent = entry.getPlainTextContent();
			List<String> sTakeAways = processTakeAways(sContent);

			if (sTakeAways.size() > 0) {
				lstTakeAways.addAll(sTakeAways);
			}

			long duration = 0;
			When when = entry.getTimes().get(0);
			String sStartTime = "--";
			String sEndTime = "--";
			if (when != null) {
				DateTime start = when.getStartTime();
				sStartTime = start.toUiString();
				DateTime end = when.getEndTime();
				sEndTime = end.toUiString();
				duration = end.getValue() - start.getValue(); // in ms
				duration /= (1000 * 60); // ms to seconds * seconds to minutes
			}

			System.out.println(i + "\t" + entry.getTitle().getPlainText()
					+ "\t" + sStartTime + "-" + sEndTime + "\tduration "
					+ duration / 60 + "h " + duration % 60 + "m");
			totalDuration += duration;
		}

		System.out.println("total duration " + totalDuration / 60 + "h "
				+ totalDuration % 60 + "m");
		DecimalFormat formatter = new DecimalFormat("###.##");
		double percentComplete = totalDuration
				/ (double) (weeks * minutesInWeek) * 100.0;
		System.out.println(formatter.format(percentComplete) + "% Complete ");

		for (int i = 0; i < lstTakeAways.size(); ++i) {
			System.out.println("takeAway: " + lstTakeAways.get(i));
		}
	}

	private static List<String> processTakeAways(String content) {
		LinkedList<String> lstRet = new LinkedList<String>();
		int startIndex = content.indexOf("<takeAway>");
		int endIndex = content.indexOf("</takeAway>", startIndex);
		while (startIndex != -1 && endIndex != -1) {
			String sTakeAway = content.substring(startIndex + 10, endIndex);
			lstRet.add(sTakeAway);
			startIndex = content.indexOf("<takeAway>", endIndex + 11);
			if (startIndex != -1) {
				endIndex = content.indexOf("</takeAway>", startIndex);
			}
		}

		return lstRet;
	}
}
